package com.infore.dataReceiving.netty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;  
import io.netty.channel.ChannelPipeline;  
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;  
import io.netty.handler.codec.string.StringEncoder;

@Component
@Qualifier("serverChannelInitializer")
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {  
	
	@Autowired
	StringDecoder stringDecoder;

	@Autowired
	StringEncoder stringEncoder;
	
//	@Autowired
//	LineBasedFrameDecoder lineBasedFrameDecoder;

	@Autowired
	ServerHandler serverHandler;

    
    @Override  
    protected void initChannel(SocketChannel ch) throws Exception {  
//    	System.out.println("初始化处理类！");
    	ChannelPipeline pipeline = ch.pipeline();
//    	ByteBuf delimiter = Unpooled.copiedBuffer("\r\n".getBytes());
    	pipeline.addLast("lineBasedFrameDecoder", new LineBasedFrameDecoder(1024));//new DelimiterBasedFrameDecoder(1024,delimiter)
        pipeline.addLast("decoder", stringDecoder);
        pipeline.addLast("encoder", stringEncoder);
        pipeline.addLast("handler", serverHandler);
    }  
}